//
//  GGTMoreKindViewController.m
//  GGT
//
//  Created by Vision on 14-10-31.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTMoreKindViewController.h"
#import "GGTAppDelegate.h"
#import "GGTTeamBuyKindModel.h"
#import "TFStackingSectionsTableView.h"
#import "TFStackingSectionsTableView-Prefix.pch"
#import "MBProgressHUD.h"
#import "GGTTeamBuyListViewController.h"
@interface GGTMoreKindViewController ()<DPRequestDelegate,UITableViewDataSource, UITableViewDelegate>
//保存数据用数组
@property (nonatomic, retain) NSMutableArray *GGTTeamBuyKindArr;
@property (nonatomic, retain) NSMutableArray *GGTTeamBuyBigKindArr;
@property (nonatomic, retain) NSMutableArray *GGTTeamBuySmallKindArr;
//TFStackingSectionsTableView
@property (nonatomic, retain) TFStackingSectionsTableView *GGTTeamBuyKindTableView;
//MBProgressHUD
@property (nonatomic, retain) MBProgressHUD *hud;


@end

@implementation GGTMoreKindViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.GGTTeamBuyKindArr = [NSMutableArray array];
        self.GGTTeamBuyBigKindArr = [NSMutableArray array];
        self.GGTTeamBuySmallKindArr = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    //解析数据
    NSString *url = @"v1/metadata/get_categories_with_deals";
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:nil delegate:self];

    //创建TFStackingSectionsTableView
    self.GGTTeamBuyKindTableView = [[TFStackingSectionsTableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.GGTTeamBuyKindTableView.delegate = self;
    self.GGTTeamBuyKindTableView.dataSource = self;
    [self.view addSubview:self.GGTTeamBuyKindTableView];
    [self.GGTTeamBuyKindTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    //HUB
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"获取分类信息";
    [self.hud setMode:MBProgressHUDModeIndeterminate];
    
}
#pragma mark - 豆瓣相关回调函数
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{
    NSMutableDictionary *kindDic =[result objectForKey:@"categories"];
    for (NSMutableDictionary * temp in kindDic) {
        GGTTeamBuyKindModel *kindModel = [[GGTTeamBuyKindModel alloc]initWithDictionary:temp];
        [self.GGTTeamBuyKindArr addObject:kindModel];
        [kindModel release];
    }
    for (GGTTeamBuyKindModel * temp in self.GGTTeamBuyKindArr) {
        [self.GGTTeamBuyBigKindArr addObject: temp.category_name];
        if (temp.subcategories.count == 0) {
            NSMutableArray *tempArr = [NSMutableArray array];
            NSString *tempStr = [NSString stringWithFormat:@"%@",temp.category_name];
            [tempArr addObject:tempStr];
            [self.GGTTeamBuySmallKindArr addObject:tempArr];
        } else {
        [self.GGTTeamBuySmallKindArr addObject:temp.subcategories];
        }
    }
  
    [self.GGTTeamBuyKindTableView reloadData];
    [self.hud hide:YES];
}
#pragma mark - tableView相关回调
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.GGTTeamBuyBigKindArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.GGTTeamBuySmallKindArr[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSArray *teams = self.GGTTeamBuySmallKindArr[indexPath.section];
    cell.textLabel.text = teams[indexPath.row];

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.GGTTeamBuyBigKindArr[section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
    UILabel *label = [UILabel new];
    label.text = [@"  " stringByAppendingString:self.GGTTeamBuyBigKindArr[section]];
    label.backgroundColor = [UIColor colorWithWhite:0.97f alpha:1.0];
    label.textColor = [UIColor colorWithWhite:0.13f alpha:1.0];
    label.font = [UIFont boldSystemFontOfSize:14.0f];
    return label;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTTeamBuyListViewController *teamBuyVC = [[GGTTeamBuyListViewController alloc]init];
    NSArray *teams = self.GGTTeamBuySmallKindArr[indexPath.section];
    teamBuyVC.str = teams [indexPath.row];
    [self.navigationController pushViewController:teamBuyVC animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
